import os

from pynput import mouse, keyboard
import threading
import time

from src.config.configLoader import ConfigLoader
from src.modules.bar.barResources import BarResources
from src.modules.bar.barStatus import BarStatus
from src.modules.bar.barValue import BarValue


from src.modules.gameWindow import GameWindow
from src.modules.skill import Skill

# 标记鼠标侧键1是否被按下
side_button_pressed = False

# 创建一个键盘控制器
keyboard_controller = keyboard.Controller()

mouse_controller = mouse.Controller()

# 创建一个事件对象
key_event = threading.Event()


def mouse_listener():
    global side_button_pressed
    with mouse.Listener(on_click=on_click) as listener:
        listener.join()


# 侧键启动信号监听
def on_click(x, y, button, pressed):
    global side_button_pressed
    if button == mouse.Button.x2 and pressed:
        # side_button_pressed = pressed
        side_button_pressed = not side_button_pressed
        if side_button_pressed:
            key_event.set()  # 设置事件
        else:
            key_event.clear()  # 清除事件


# 主循环进行键盘技能控制
def key_press_loop():
    global side_button_pressed

    # 获取当前文件所在目录
    current_dir = os.path.dirname(os.path.abspath(__file__))

    gameWindow = GameWindow()

    # === 启动时加载配置 ===
    config_loader = ConfigLoader(config_path="config.json", project_root=current_dir)

    # 选择分辨率模式
    resolution = "2k"  # 或 "2k"

    # === 初始化各个模块 ===

    bar = BarValue(gameWindow, config_loader.get_section("barValue", resolution))
    bar_status = BarStatus(gameWindow, config_loader.get_section("barStatus", resolution))
    bar_resources = BarResources(gameWindow, config_loader.get_section("barResources", resolution))
    specialSkill = Skill(gameWindow, config_loader.get_section("skill", resolution), "specialSkill")
    skill1 = Skill(gameWindow, config_loader.get_section("skill", resolution), "specializeSkill1")
    skill2 = Skill(gameWindow, config_loader.get_section("skill", resolution), "specializeSkill2")
    skill4 = Skill(gameWindow, config_loader.get_section("skill", resolution), "specializeSkill4")

    print('初始化完成')
    while True:
        key_event.wait()  # 等待事件被设置

        print('启动')

        while side_button_pressed:

            if skill2.is_ready():
                keyboard_controller.tap('3')
                time.sleep(0.2)
                keyboard_controller.tap('3')
                time.sleep(0.2)

            print(bar.get_value())
            if bar.get_value() > 59:
                keyboard_controller.tap('1')
                time.sleep(0.2)
                keyboard_controller.tap('1')
                print('生命绽放')
                time.sleep(1.8)

            if skill1.get_value() > 0:
                keyboard_controller.tap('2')
                time.sleep(0.2)
                keyboard_controller.tap('2')
                print('狂野之种')
                time.sleep(0.5)

            mouse_controller.click(mouse.Button.left, 1)
            print('普攻')

        key_event.clear()  # 清除事件


# 启动鼠标监听线程
mouse_thread = threading.Thread(target=mouse_listener, daemon=True)
mouse_thread.start()

# 启动按键循环线程
key_press_thread = threading.Thread(target=key_press_loop, daemon=True)
key_press_thread.start()

# 保持主程序运行
mouse_thread.join()
key_press_thread.join()
